#include "types.h"
#include "sw_interrupt.h"
#include "loader.h"
#include "ksem.h"
#include "os_io.h"
#include "os_queue.h"
#include "os_timers.h"
#include "timers.h"
#include "scheduler.h"
#include "emac.h"
#include "os_dev.h"

static void (*handler[MAX_SW_INTERRUPTS])(uint32_t data);


void _dummy_interrupt(uint32_t data)
{
  printf("Dummy interrupt handler called\n");
  return;
}


void initialize_sw_interrupts(void)
{
	uint16_t i = 0;
	for (i = 0; i < MAX_SW_INTERRUPTS; i++)
	{
		handler[i] = NULL;
		
	}

	handler[SW_INT_EXIT_PROCESS] = &exit_process;
	handler[SW_INT_EXEC_PROCESS] = &exec_process;
	handler[SW_INT_SEMAPHORE_LOCK] = &lock_ksem;
	handler[SW_INT_SEMAPHORE_UNLOCK ] = &unlock_ksem;
	handler[SW_INT_SEMAPHORE_TRYLOCK ] = &trylock_ksem;
	handler[SW_INT_CREATE_QUEUE] = _create_queue;
	handler[SW_INT_WAIT_QUEUE] = _wait_queue;
	handler[SW_INT_DELETE_QUEUE] = _delete_queue;
	handler[SW_INT_ENQ_QUEUE] = _enq_queue;
	handler[SW_INT_DUMMY_INTERRUPT] = _dummy_interrupt;
	handler[SW_INT_TIMER_START] = &start_timer;
	handler[SW_INT_EMAC_CFG] = &emac_cfg;
	handler[SW_INT_QUEUE_LOOKUP] = &_lookup_queue;
	handler[SW_INT_DEVICE_OPEN] = &open_device;
	handler[SW_INT_DEVICE_CLOSE] = &close_device;
	handler[SW_INT_DEVICE_POST] = &post_to_dev_owner;
	handler[SW_INT_TIMER_CANCEL] = &cancel_timer;
	handler[SW_INT_RESET_PROCESSOR] = &reset_processor;

	return;
}

void sw_interrupt_handler(uint32_t num, uint32_t data)
{
       
	if (num >= MAX_SW_INTERRUPTS) {
	    return;
	}


	if (num == SW_INT_DELETE_QUEUE) {
	    printf("Received a request to delete the queue: 0x%x\n", data);
	}

	if (handler[num] == NULL) {
		printf("Handler not registered for SW interrupt: 0x%x\n", num);
	} else {
		(handler[num])(data);
	}

	return;
}
